/**
 * @ClassName: CustomerView
 * @Description: 项目入口
 * @Author 张居然
 * @Create 2024/1/27 17:26
 * @Version 1.0
 */
public class CustomerView {
    private CustomerList cList = new CustomerList(10);

    public CustomerView() {
        Customer cust = new Customer("王春梅", "女", 18, "15512341234", "wangwc@163.com");
        cList.addCustomer(cust);
    }

    /**
     * 主界面
     */
    public void enterMainMenu() {
//        CMUtility cmUtility = new CMUtility();
        boolean flag = true;

        while (flag) {
            System.out
                    .println("\n------------------------------拼电商客户管理系统----------------------------\n");
            System.out.println("                   1 添 加 客 户");
            System.out.println("                   2 修 改 客 户");
            System.out.println("                   3 删 除 客 户");
            System.out.println("                   4 客 户 列 表");
            System.out.println("                   5 退       出\n");
            System.out.print("                   请选择(1-5)：");

            char selection = CMUtility.readMenuSelection();
            System.out.println();
            switch (selection) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomers();
                    break;
                case '5':
                    System.out.println("是否确认退出(y/n):");
                    char rcs = CMUtility.readConfirmSelection();
                    if (rcs == 'Y')
                        flag = false;
                    break;
            }
        }
    }

    /**
     * 添加客户
     */
    private void addNewCustomer() {
        System.out.println("---------------------添加客户---------------------");
        System.out.print("姓名: ");
        String name = CMUtility.readString(15);

        System.out.print("性别: ");
        String gender = CMUtility.readString(4);

        System.out.print("年龄: ");
        int age = CMUtility.readInt();

        System.out.print("电话: ");
        String phone = CMUtility.readString(15);

        System.out.print("邮箱: ");
        String email = CMUtility.readString(15);

        Customer customer = new Customer(name, gender, age, phone, email);
        if (cList.addCustomer(customer))
            System.out.println("---------------------添加完成---------------------");
        else
            System.out.println("--------------------------记录已满,无法添加---------------------------");
    }

    /**
     * 修改客户信息
     */
    private void modifyCustomer() {
        System.out.println("-------------------修改客户信息--------------------");
        int index = 0;
        Customer cust = null;
        for (; ; ) {
            System.out.println("请选择待修改客户编号(编号为大于等于1的整数,退出请按-1):");
            index = CMUtility.readInt();
            if (index == -1)
                return;
            if (index < 1 || index > cList.getTotal()) {
                System.out.println("编号有误,请重新输入！");
                continue;
            }
            cust = cList.getCustomer(index - 1);

            System.out.print("姓名(" + cust.getName() + "): ");
            String name = CMUtility.readString(15,cust.getName());

            System.out.print("姓别(" + cust.getGender() + "): ");
            String gender = CMUtility.readString(4,cust.getGender());

            System.out.print("年龄(" + cust.getAge() + "): ");
            int age = CMUtility.readInt(cust.getAge());

            System.out.print("电话(" + cust.getPhone() + "): ");
            String phone = CMUtility.readString(15,cust.getPhone());

            System.out.print("邮箱(" + cust.getEmail() + "): ");
            String email = CMUtility.readString(15,cust.getEmail());

            cust = new Customer(name,gender,age,phone,email);
            boolean flag = cList.replaceCustomer(index - 1,cust);
            if (flag)
                System.out.println("---------------------修改完成---------------------");
            else
                System.out.println("---------------------修改完成---------------------");
        }
    }

    /**
     * 删除客户
     */
    private void deleteCustomer() {
        System.out.println("---------------------删除客户---------------------");
        System.out.print("请选择待删除客户编号(-1退出): ");
        int index = CMUtility.readInt();
        if (index == -1) {
            return;
        }
        if (cList.deleteCustomer(index))
            System.out.println("---------------------删除成功---------------------");
        else
            System.out.println("-------------删除失败，请检查编号是否正确-------------");
    }

    /**
     * 显示客户列表
     */
    private void listAllCustomers() {
        System.out.println("---------------------------客户列表---------------------------");
        Customer[] allCustomers = cList.getAllCustomers();
        System.out.println("编号\t\t姓名\t\t" + "性别\t\t" + "年龄\t\t" + "电话\t\t\t\t" + "邮箱");
        if (allCustomers.length == 0) {
            System.out.println("没有客户信息！");
            return;
        }
        for (int i = 0; i < allCustomers.length; i++) {
            System.out.println((i + 1) + "\t\t" + allCustomers[i].getName() + "\t"
                    + allCustomers[i].getGender() + "\t\t" + allCustomers[i].getAge() + "\t\t"
                    + allCustomers[i].getPhone() + "\t\t" + allCustomers[i].getEmail());
        }
        System.out.println("-------------------------客户列表完成-------------------------");
    }


    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMainMenu();
    }
}
